package mpcs.view
{
	import flash.events.Event;
	
	import flexlib.mdi.containers.MDIWindow;
	
	import mpcs.model.vo.GlobalVO;
	import mpcs.utils.ComponentManager;
	import mpcs.utils.MDIWindowPools;
	import mpcs.utils.MDIWindowType;
	import mpcs.utils.MapManager;
	import mpcs.view.components.DesktopPanel;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class DesktopMediator extends Mediator {
		
		public static const NAME:String = "DesktopMediator";
		
		public function DesktopMediator(viewComponent:DesktopPanel) {
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void {
			desktopPanel.addEventListener( DesktopPanel.CLICK_LOCATION, onLocation );
			desktopPanel.addEventListener( DesktopPanel.CLICK_USERINFO, onUserinfo );
			desktopPanel.addEventListener( DesktopPanel.CLICK_HISTORY, onHistory );
			desktopPanel.addEventListener( DesktopPanel.CLICK_MORES, onMores );
			desktopPanel.addEventListener( DesktopPanel.CLICK_GOOGLE, onGoogle );
		}
		
		/**打开实时位置窗口请求**/
		private function onLocation( event:Event ):void {
			trace("请求实时位置窗口 ... ");
			if( ComponentManager.isopen_location ) {
				trace("恢复实时位置窗口 ... ");
				if(ComponentManager.locationMDIWindow == null)
					return;
				ComponentManager.locationMDIWindow.maximize();
				ComponentManager.locationMDIWindow.setFocus();
				return;
			}else{
				ComponentManager.isopen_location = true;
				var mapWin:MDIWindow = MDIWindowPools.getInstance().getMDIWindow( MDIWindowType.MAP_TYPE );
				mapWin.id = "mapMDI";
				mapWin.title = "实时位置";
				desktopPanel.mdiWindow.mdiCanvas.windowManager.add( mapWin );
				
				ComponentManager.locationMDIWindow = mapWin;
				trace("新建实时位置窗口 ... ");
			}
		}
		
		/**打开用户信息窗口请求**/
		private function onUserinfo( event:Event ):void {
			trace("请求用户信息窗口 ... ");
			if( ComponentManager.isopen_userinfo ) {
				trace("恢复用户信息窗口 ... ");
				if(ComponentManager.userinfoMDIWindow == null)
					return;
				ComponentManager.userinfoMDIWindow.maximize();
				ComponentManager.userinfoMDIWindow.setFocus();
				return;
			}else{
				ComponentManager.isopen_userinfo = true;
				/////////////////////////////////////////////////////////////////////////
				var infoWin:MDIWindow = MDIWindowPools.getInstance().getMDIWindow( MDIWindowType.USERINFO_TYPE );
				infoWin.id = "userinfoMDI";
				infoWin.title = "个人信息";
				desktopPanel.mdiWindow.mdiCanvas.windowManager.add( infoWin );
				ComponentManager.userinfoMDIWindow = infoWin;
				/////////////////////////////////////////////////////////////////////////
				trace("新建用户信息窗口 ... ");
			}
		}
		
		/**打开历史位置数据窗口请求**/
		private function onHistory( event:Event ):void {
			trace("请求历史位置数据窗口 ... ");
			if( ComponentManager.isopen_history ) {
				trace("恢复历史位置窗口 ... ");
				if(ComponentManager.historyMDIWindow == null)
					return;
				ComponentManager.historyMDIWindow.maximize();
				ComponentManager.historyMDIWindow.setFocus();
				return;
			}else{
				ComponentManager.isopen_history = true;
				/////////////////////////////////////////////////////////////////////////
				var historyWin:MDIWindow = MDIWindowPools.getInstance().getMDIWindow( MDIWindowType.HISTORY_TYPE );
				historyWin.id = "historyMDI";
				historyWin.title = "历史数据";
				desktopPanel.mdiWindow.mdiCanvas.windowManager.add( historyWin );
				ComponentManager.historyMDIWindow = historyWin;
				/////////////////////////////////////////////////////////////////////////
				trace("新建历史位置窗口 ... ");
			}
		}
		
		/**打开更多服务窗口请求**/
		private function onMores( event:Event ):void {
			trace("请求更多服务窗口 ... ");
			if( ComponentManager.isopen_mores ) {
				trace("恢复更多服务窗口 ... ");
				if(ComponentManager.moresMDIWindow == null)
					return;
				ComponentManager.moresMDIWindow.maximize();
				ComponentManager.moresMDIWindow.setFocus();
				return;				
			}else{
				ComponentManager.isopen_mores = true;
				/////////////////////////////////////////////////////////////////////////
				var moresWin:MDIWindow = MDIWindowPools.getInstance().getMDIWindow( MDIWindowType.MORESERVICE_TYPE );
				moresWin.id = "moresMDI";
				moresWin.title = "更多服务";
				desktopPanel.mdiWindow.mdiCanvas.windowManager.add( moresWin );
				ComponentManager.moresMDIWindow = moresWin;
				/////////////////////////////////////////////////////////////////////////
				trace("新建更多服务窗口 ... ");
			}
		}
		
		/**打开Google搜索窗口请求**/
		private function onGoogle( event:Event ):void {
			trace("请求Google搜索窗口 ... ");
			if( ComponentManager.isopen_google ) {
				trace("恢复Google搜索窗口 ... ");
				if(ComponentManager.googleMDIWindow == null)
					return;
				ComponentManager.googleMDIWindow.maximize();
				ComponentManager.googleMDIWindow.setFocus();
				return;
			}else{
				ComponentManager.isopen_google = true;
				/////////////////////////////////////////////////////////////////////////
				var googleWin:MDIWindow = MDIWindowPools.getInstance().getMDIWindow( MDIWindowType.GOOGLE_TYPE );
				googleWin.id = "googleMDI";
				googleWin.title = "Google搜索";
				desktopPanel.mdiWindow.mdiCanvas.windowManager.add( googleWin );
				ComponentManager.googleMDIWindow = googleWin;
				/////////////////////////////////////////////////////////////////////////
				trace("新建Google搜索窗口 ... ");
			}
		}
		
		override public function listNotificationInterests():Array {
			return [];
		}
		
		override public function handleNotification( note:INotification ):void {
			
		}
		
		private function get desktopPanel ():DesktopPanel {
			return viewComponent as DesktopPanel;
		}
	}
}